//
//  ItemImageExtension.m
//  VietQuan
//
//  Created by hainguyen on 11/1/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "ItemImageExtension.h"
#import "AppDelegate_Shared.h"
#import "CoreDataHelper.h"

@implementation ItemImage(Extension)

+ (BOOL) initItemImage{
	NSError *err = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if(managedObjectContext == nil){
		return NO;
	}
	
	NSMutableArray *itemImgArray = [[[NSMutableArray alloc] init] autorelease];
	NSMutableDictionary *itemImgDict = [[NSMutableDictionary alloc] init];
	[itemImgDict setObject:[NSNumber numberWithInt:1] forKey:@"id"];
	[itemImgDict setObject:@"Ốc Bưu Nhồi Thịt" forKey:@"name"];
	[itemImgDict setObject:@"oc_buu_nhoi_thit.jpg" forKey:@"data"];
	[itemImgDict setObject:[NSNumber numberWithInt:3] forKey:@"itemId"];
	[itemImgArray addObject:itemImgDict];
	[itemImgDict release];
	
	itemImgDict = [[NSMutableDictionary alloc] init];
	[itemImgDict setObject:[NSNumber numberWithInt:2] forKey:@"id"];
	[itemImgDict setObject:@"Bún chả cá Miền Trung" forKey:@"name"];
	[itemImgDict setObject:@"bun_cha_ca_mien_trung.jpg" forKey:@"data"];
	[itemImgDict setObject:[NSNumber numberWithInt:4] forKey:@"itemId"];
	[itemImgArray addObject:itemImgDict];
	[itemImgDict release];
	
	itemImgDict = [[NSMutableDictionary alloc] init];
	[itemImgDict setObject:[NSNumber numberWithInt:3] forKey:@"id"];
	[itemImgDict setObject:@"Hủ Tiếu Trương Định" forKey:@"name"];
	[itemImgDict setObject:@"hu_tieu_truong_dinh.jpg" forKey:@"data"];
	[itemImgDict setObject:[NSNumber numberWithInt:1] forKey:@"itemId"];
	[itemImgArray addObject:itemImgDict];
	[itemImgDict release];
	
	itemImgDict = [[NSMutableDictionary alloc] init];
	[itemImgDict setObject:[NSNumber numberWithInt:4] forKey:@"id"];
	[itemImgDict setObject:@"Nem Ninh Hòa" forKey:@"name"];
	[itemImgDict setObject:@"nem_ninh_hoa.jpg" forKey:@"data"];
	[itemImgDict setObject:[NSNumber numberWithInt:2] forKey:@"itemId"];
	[itemImgArray addObject:itemImgDict];
	[itemImgDict release];
	
	for (int i = 0; i < [itemImgArray count]; i++) {
		NSMutableDictionary *dict = [itemImgArray objectAtIndex:i];
		ItemImage *itemImage = [NSEntityDescription insertNewObjectForEntityForName:@"ItemImage" inManagedObjectContext:managedObjectContext];
		itemImage.id = [dict objectForKey:@"id"];
		itemImage.name = [dict objectForKey:@"name"];
		itemImage.data = [NSData dataWithContentsOfFile:[dict objectForKey:@"data"]];
		itemImage.itemId = [dict objectForKey:@"itemId"];
	} 
	
	[managedObjectContext save:&err];
	return YES;
}

+ (NSArray *) getItemImages{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil){
		return nil;	
	}
	NSArray *lists;
	@try {
		lists = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"RestaurantImage" :nil :nil :NO :managedObjectContext];
	}@catch (NSException * e) {
		NSLog(@"%@",[e description]);
    }
	
	return lists;	
}
+ (ItemImage *) getItemImageWithItemImageId:(int)itemImageId{
	ItemImage* itemImage = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return nil;
	}	
	NSMutableArray *itemImageArray;
	NSPredicate *predicate = [NSPredicate predicateWithFormat: @"id = %d", itemImageId];	
	@try {
		itemImageArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"ItemImage" :predicate :nil :NO :managedObjectContext];
		
		if ([itemImageArray count] != 0)
		{
			itemImage = [itemImageArray objectAtIndex:0];
		}
	} @catch (NSException * e) {
		NSLog(@"error: %@", [e description]);
	}
	
	return itemImage;
}
+ (NSArray *) getItemImageWithItem:(int)itemId{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return nil;
	}	
	NSMutableArray *itemImageArray;
	NSPredicate *predicate = [NSPredicate predicateWithFormat: @"itemId = %d", itemId];	
	@try {
		itemImageArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"ItemImage" :predicate :nil :NO :managedObjectContext];
		
	} @catch (NSException * e) {
		NSLog(@"error: %@", [e description]);
	}
	
	return itemImageArray;
}
@end
